package com.internal.Internalhibernate.server.dao;

// Generated Feb 24, 2012 1:32:12 PM by Hibernate Tools 3.3.0.GA

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.internal.Internalhibernate.client.AdmNationalities;
import com.internal.Internalhibernate.server.SessionFactoryUtil;

/**
 * Home object for domain model class AdmNationalities.
 * 
 * @see com.internal.Internalhibernate.client.AdmNationalities
 * @author Hibernate Tools
 */
public class AdmNationalitiesDAO {
	private static final Log log = LogFactory.getLog(AdmNationalitiesDAO.class);

	private final SessionFactory sessionFactory = SessionFactoryUtil
			.getSessionFactory();

	public void persist(AdmNationalities transientInstance) {
		log.debug("persisting AdmNationalities instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.persist(transientInstance);
			trans.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(AdmNationalities instance) {
		log.debug("attaching dirty AdmNationalities instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.saveOrUpdate(instance);
			trans.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(AdmNationalities instance) {
		log.debug("attaching clean AdmNationalities instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.lock(instance, LockMode.NONE);
			trans.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(AdmNationalities persistentInstance) {
		log.debug("deleting AdmNationalities instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.delete(persistentInstance);
			trans.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("delete failed", re);
			throw re;
		}
	}

	public AdmNationalities merge(AdmNationalities detachedInstance) {
		log.debug("merging AdmNationalities instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			AdmNationalities result = (AdmNationalities) session
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("merge failed", re);
			throw re;
		}
	}

	public AdmNationalities findById(short id) {
		log.debug("getting AdmNationalities instance with id: " + id);
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			AdmNationalities instance = (AdmNationalities) session.get(
					"com.internal.Internalhibernate.client.AdmNationalities", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findAll() {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from AdmNationalities");
		return q.list();
	}

	public String getCountryName(long id) {
		try {
			Session session = sessionFactory.getCurrentSession();
			session.beginTransaction();
			Query q = session
					.createQuery("select nationality from AdmNationalities where natId = '"
							+ id + "'");
			return (String) q.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get nationality name by id failed :", re);
			throw re;
		}
	}
}
